package com.lc.midi;

import com.lc.Main;
import com.lc.midi.script.Config;
import org.apache.commons.cli.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class CommandHelper {
    public Config config = new Config();
    public String scriptDir = "script";
    public List<String> midis = new ArrayList<>();

    public CommandHelper(String[] args) throws Exception {
        Options options = new Options();
        Option width = new Option("w", "width", true, "分辨率的宽,默认1920");
        width.setRequired(false);
        width.setType(Integer.TYPE);
        Option height = new Option("h", "height", true, "分辨率的高,默认1080");
        height.setRequired(false);
        height.setType(Integer.TYPE);
        Option waitStartTime = new Option("st", "waitStartTime", true, "开始等待时间,默认1000ms");
        waitStartTime.setRequired(false);
        waitStartTime.setType(Integer.TYPE);
        Option maxBlankTime = new Option("bt", "maxBlankTime", true, "两个相邻音最大时间差,默认2000ms");
        maxBlankTime.setRequired(false);
        maxBlankTime.setType(Integer.TYPE);
        Option kind = new Option("k", "kind", true, "脚本类型,默认js");
        kind.setRequired(false);
        Option imei = new Option("i", "imei", true, "设备的IMEI码,默认没有");
        imei.setRequired(false);
        Option ts = new Option("ts", false, "是否是脚本播放器版,默认不是");
        ts.setRequired(false);
        Option pianoApp = new Option("pa", "pianoApp", true, "钢琴软件,默认明日之后");
        pianoApp.setRequired(false);
        Option dir = new Option("dir", "scriptDir", true, "存放脚本的文件夹，默认script");
        dir.setRequired(false);
        Option color = new Option("c", "color", false, "是否显示不同颜色，默认否");
        color.setRequired(false);
        options.addOption(width);
        options.addOption(height);
        options.addOption(waitStartTime);
        options.addOption(maxBlankTime);
        options.addOption(kind);
        options.addOption(imei);
        options.addOption(ts);
        options.addOption(pianoApp);
        options.addOption(dir);
        options.addOption(color);

        CommandLineParser parser = new DefaultParser();
        CommandLine commandLine;
        try {
            commandLine = parser.parse(options, args);
        } catch (ParseException e) {
            new HelpFormatter().printHelp("帮助", options);
            throw new Exception();
        }
        if (commandLine.hasOption(width.getOpt())) {
            config.setWidth(Integer.parseInt(commandLine.getOptionValue(width.getOpt())));
        }
        if (commandLine.hasOption(height.getOpt())) {
            config.setHeight(Integer.parseInt(commandLine.getOptionValue(height.getOpt())));
        }
        if (commandLine.hasOption(waitStartTime.getOpt())) {
            config.setWaitStartTime(Integer.parseInt(commandLine.getOptionValue(waitStartTime.getOpt())));
        }
        if (commandLine.hasOption(maxBlankTime.getOpt())) {
            config.setMaxBlankTime(Integer.parseInt(commandLine.getOptionValue(maxBlankTime.getOpt())));
        }
        if (commandLine.hasOption(kind.getOpt())) {
            config.setKind(commandLine.getOptionValue(kind.getOpt()));
        }
        if (commandLine.hasOption(imei.getOpt())) {
            config.setImei(commandLine.getOptionValue(imei.getOpt()));
        }
        if (commandLine.hasOption(ts.getOpt())) {
            config.setTs(true);
        }
        if (commandLine.hasOption(pianoApp.getOpt())) {
            config.setPianoApp(commandLine.getOptionValue(pianoApp.getOpt()));
        }
        if (commandLine.hasOption("help")) {
            new HelpFormatter().printHelp("帮助", options);
        }
        if (commandLine.hasOption(dir.getOpt())) {
            scriptDir = commandLine.getOptionValue(dir.getOpt());
        }
        if (commandLine.hasOption(color.getOpt())) {
            Main.logger.color = true;
        }
        Main.logger.d(config.toString());
        midis.addAll(Arrays.asList(commandLine.getArgs()));
        Main.logger.i("待转换的mid文件有" + midis.size() + "个");
    }
}
